<?php
/**
 * @version SVN: $Id: convert.php 1173 2010-04-25 19:45:54Z elkuku $
 * @package
 * @subpackage
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath {@link http://www.nik-it.de}
 * @author     Created on 22.04.2010
 * @license    GNU/GPL, see JROOT/LICENSE.php
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

ecrStylesheet('diff');


$fixForJVersion = '1.6';
$languageTags = $this->converter->getKnownLanguageTags();

$selected_file = JRequest::getVar('selected_file');

$paths = $this->project->getLanguagePaths();

echo $this->menuBoxes['file'];

$color =(count($this->badDefinitions)) ? 'yellow' : 'green';
echo '<div class="ecr_menu_box" style="margin-left: 0.3em; background-color: '.$color.';">';
printf('Found <b>%d</b> errors in project', count($this->badDefinitions));
echo '</div>';

echo $this->menuBox('php_show_all', JText::_('Show all PHP code'));
echo $this->menuBox('convert_quotes', JText::_('Convert quotes'));

echo '<div style="padding-top: 1em; padding-bottom: 1em;">';

echo $this->menuBox('convert_forbidden', JText::_('Convert forbidden keys'));
echo $this->menuBox('remove_bads', JText::_('Remove bad charcters'));
echo $this->menuBox('convert_white_space', JText::_('Convert white space'));
echo $this->menuBox('add_prefix', JText::_('Add prefix'));
echo $this->menuBox('convert_all_caps', JText::_('Convert to all caps'));
echo '<br />';
echo '<br />';

if($selected_file
&& isset($this->fileList[JPATH_ROOT.DS.$selected_file])
&& $this->fileList[JPATH_ROOT.DS.$selected_file])
{
    echo '<div class="ecr_menu_box" style="margin-left: 0.3em; background-color: yellow;">';
    echo sprintf('Found <b>%d</b> errors in %s', $this->fileList[JPATH_ROOT.DS.$selected_file], $selected_file);
    echo '</div>';
}

if($this->diff)
{
    echo '<span style="margin-left: 0.3em;" onclick="submitform(\'do_convert\');" class="ecr_button img icon-16-rename">';
    echo JText::_('Convert code and language file').'</span>';
}
else
{
    echo '<span style="margin-left: 0.3em;" onclick="submitform(\'do_convert\');" class="ecr_button img icon-16-rename">';
    echo JText::_('Convert language file').'</span>';
    echo '</div>';
}

echo $this->menuBoxes['file_errors'];

if($this->diff)
{

    echo '</div>';
    ?>
<h1>PHP</h1>
<table class="diff" width="100%" style="border: 1px solid gray;">
	<tr>
		<th colspan="2" width="50%" style="background-color: #fff;"><?php echo JText::sprintf('Original'); ?></th>
		<th colspan="2" width="50%" style="background-color: #ffc;"><?php echo JText::sprintf('Fixed for %s', $fixForJVersion); ?></th>
	</tr>
	<?php echo $this->diff; ?>
</table>

	<?php
}

?>
<h1>INI</h1>
<?php
$headersButtons = '';

$languageFilesFound = false;

$buttons = '';
$tables = '';

foreach($languageTags as $tag) :
$sTag = substr($tag, 0, 2);
$path = $paths[$this->scope].DS.'language'.DS.$tag;

$fileName = $tag.'.'.$this->project->getLanguageFileName($this->scope);

if(JFile::exists($path.DS.$fileName)) :
$fileContents = JFile::read($path.DS.$fileName);
else :
continue;
endif;

$languageFilesFound = true;
$lines = explode("\n", $fileContents);

$newLines = $this->converter->cleanLines($lines);
$newLines = $this->converter->cleanLangFileErrors($newLines, array_keys($this->fileErrors));

$newFileContents = implode("\n", $newLines);

$buttons .= '
<div class="ecr_button img icon-16-add" style="float: left;" onclick="langfile_'.$sTag.'.toggle();">'.$tag.'</div>';

$tables .= '
<div id="langfile_'.$sTag.'" style="float: left">

<table cellpadding="0" cellspacing="0"
	style="background-color: #fff; border: 1px solid gray;">
	<tr>
		<th style="border-bottom: 1px solid gray;" colspan="4">'
		.JText::sprintf('Language file: %s', $tag)
		.'</th>
	</tr>
	<tr>
		<th width="50%" style="border-bottom: 1px solid gray; border-right: 1px solid gray;" colspan="2">'
		.JText::_('Original')
		.'</th>
		<th width="50%" style="border-bottom: 1px solid gray; background-color: #ffc;" colspan="1">'
		.JText::sprintf('Fixed for %s', $fixForJVersion)
		.'</th>
	</tr>';

		for($i = 0; $i < count($lines); $i++) :
		$origParts = $this->converter->splitLine($lines[$i]);
		$newParts = $this->converter->splitLine($newLines[$i]);

		$tables .= '<tr>';
		$tables .= '<td align="right" style="border-bottom: 1px solid gray; background-color: #eee;">'.$i.'</td>';
		$bgColor = '';
		$line = $lines[$i];

		if($origParts[1] != '') :
		if($origParts[0] == '') :
		//-- Comment
		$bgColor = ' background-color: #cff;';
		else :
		$color =($newParts[0] != $origParts[0]) ? 'red' : 'blue';

		//-- key = value
		$line = '<span style="color: '.$color.';">'.$origParts[0].'</span>';
		$line .= ' = <span style="color: green;">'.htmlspecialchars($origParts[1]).'</span>';
		endif;
		endif;

		$color =($lines[$i] != $newLines[$i]) ? ' background-color: #ddffdd;' : $bgColor;
		$tables .= '<td style="border-bottom: 1px solid gray; border-right: 1px solid gray;'.$color.'">';
		$tables .= $line;
		$tables .= '</td>';

		$bgColor = '';
		$line = $newLines[$i];

		if($newParts[1] != '') :
		if($newParts[0] == '') :
		//-- Comment
		$bgColor = ' background-color: #cff;';
		else :
		$color =($newParts[0] != $origParts[0]) ? 'red' : 'blue';

		//-- key value
		$line = '<span style="color: '.$color.';">'.$newParts[0].'</span> = <span style="color: green;">'.htmlspecialchars($newParts[1]).'</span>';
		endif;
		endif;

		$color =($lines[$i] != $newLines[$i]) ? ' background-color: #ffc;' : $bgColor;
		$tables .= '<td style="border-bottom: 1px solid gray; border-right: 1px solid gray;'.$color.'">';
		$tables .= $line;
		$tables .= '</tr>';
		endfor;
		$tables .= '
</table>
</div>
<script type="text/javascript">
	var langfile_'.$sTag.' = new Fx.Slide(\'langfile_'.$sTag.'\');
	langfile_'.$sTag.'.hide();
</script>';

		endforeach;

		echo $buttons;
		echo '<div style="clear: both"></div>';
		echo $tables;

		if( ! $languageFilesFound)
		{
		    ecrHTML::displayMessage(JText::_('No language files found in selected scope'), 'notice');
		}
